//
//  UIButton+Block.m
//  HelpThePoor
//
//  Created by van7ish on 2016/11/26.
//  Copyright © 2016年 van7ish. All rights reserved.
//

#import "UIButton+Block.h"

#import <objc/runtime.h>

static id key;

@implementation UIButton (Block)

- (void)initWithBlock:(ClickActionBlock)clickBlock for:(UIControlEvents)event{
    objc_setAssociatedObject(self, &key, clickBlock, OBJC_ASSOCIATION_COPY_NONATOMIC);
    [self addTarget:self action:@selector(goAction:) forControlEvents:event];
}

- (void)goAction:(UIButton *)sender{
    ClickActionBlock block = (ClickActionBlock)objc_getAssociatedObject(self, &key);
    if (block) {
        block(sender);
    }
}

@end
